/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package tk.mybatis.springboot.api.controller;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.share.util.WeixinUtil;
import tk.mybatis.springboot.wexin.bean.AccessToken;
import tk.mybatis.springboot.wexin.bean.Userinfo;
import tk.mybatis.springboot.wexin.service.CoreService;
import tk.mybatis.springboot.wexin.service.WeixinService;

/**
 * @author liujh
 */
@Controller
@RequestMapping("/weixin")
public class WexinController extends BaseController{

    @Autowired
    CoreService coreService;
    @Autowired
    WeixinService weixinService;
    
    @RequestMapping(method = RequestMethod.GET, value = "WeixinAccess")
    @ResponseBody
    public void wexin(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    	coreService.wexGet(request, response);
    }
    //微信菜单回掉
    @RequestMapping(method = RequestMethod.POST, value = "WeixinAccess")
    @ResponseBody
    public String wexin2(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	return coreService.processRequest(request,response);
    }
    
    //微信菜单回掉
    @RequestMapping(value = "createMenu")
    @ResponseBody
    public JSONObject createMenu(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	return weixinService.createMenu();
    }
    
    @RequestMapping(value = "user")
    @ResponseBody
    public void user(HttpServletRequest request, HttpServletResponse response, String state, String code)throws Exception {  	
    	AccessToken accessToken = WeixinUtil.getAccessToken(code);
    	Userinfo userinfo = WeixinUtil.getUserinfo(accessToken);
    	HttpSession session = request.getSession();
    	session.setAttribute("userinfo", userinfo);
    	if("jiyou".equals(state)) {
    		response.sendRedirect("http://wx.ydche.com/eInsurance/actlist.html");
    	}
    }
    
    @RequestMapping(value = "getPersonInfo")
    @ResponseBody
    public Message getPersonInfo(HttpServletRequest request, HttpServletResponse response, String state, String code)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	return success("成功",userinfo);
    }
}
